package com.example.second_home_work

import android.os.Bundle
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.forEach

class MainActivity : AppCompatActivity() {
    var count=0
    val expert=ProgramExpert()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val button1=findViewById<Button>(R.id.button1)
        val button2=findViewById<Button>(R.id.button2)
        val linearLayout=findViewById<LinearLayout>(R.id.line1)
        val linearLayout2=findViewById<LinearLayout>(R.id.line2)

        val spinner = findViewById<Spinner>(R.id.spinner)
        val textView=findViewById<TextView>(R.id.textView)


        button2.setOnClickListener {
            val newTextView=TextView(this)
            count++
            newTextView.text="New TextView $count"
            newTextView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            linearLayout.addView(newTextView)
        }

        button1.setOnClickListener{
            val selectedItem=spinner.selectedItem.toString()
            textView.text=expert.getLanguage(selectedItem)
            getAllTextViewsContent(linearLayout, linearLayout2)
        }
    }
    private fun getAllTextViewsContent(linearLayout: LinearLayout, linearLayout2: LinearLayout) {
        linearLayout2.removeAllViews()
        for (i in 0 until linearLayout.childCount) {
            val view = linearLayout.getChildAt(i)
            if (view is TextView) {
                val newTextView = TextView(this)
                newTextView.text = view.text.toString()
                newTextView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply {
                    gravity = Gravity.CENTER
                }
                linearLayout2.addView(newTextView)
            }
        }
    }

}